package com.icebergteam.warfacewidget.core.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.icebergteam.warfacewidget.core.LogManager;

public class TimeDateUtils {
	
	public static final String className = TimeDateUtils.class.getCanonicalName();
	
	public static String getDateStringFromTimestamp(long timestamp, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		sdf.setTimeZone(TimeZone.getDefault());
		return sdf.format(new Date(timestamp * 1000));
	}
	
	public static long getCurrentTimestamp() {
		return new java.sql.Timestamp(new java.util.Date().getTime()).getTime();
	}
	
	public static String getDateStringFromNormalTimestamp(long timestamp, String format, Locale locale)  {
		SimpleDateFormat sdf;
		try {
			sdf = new SimpleDateFormat(format, locale);
			sdf.setTimeZone(TimeZone.getDefault());
			return sdf.format(new Date(timestamp ));
		} catch (Exception e) {
			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
			e.printStackTrace();
		}
		return String.valueOf(timestamp);
	}
	
	public static String getDateByLocale(Date date, Locale locale, String frmt)
			throws Exception {
		String format = "dd MMM yyyy HH:mm:ss";
		if(frmt!= null){
			format = frmt;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
		sdf.setTimeZone(TimeZone.getDefault());
		return sdf.format(date);
	}
	
	public static int compareToDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date1).compareTo(sdf.format(date2));
    }
	
	public static long getTimestampFromString(String format, String regexp, String src) throws Exception{

		SimpleDateFormat sdf = new SimpleDateFormat(format);
			
		Pattern patternYoutubeLink = Pattern.compile(regexp);
		Matcher matcher =  patternYoutubeLink.matcher(src);
		matcher.find(); 
		String pubdateStr = matcher.group();
		
		return sdf.parse(pubdateStr).getTime();		
	}
}